<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>房贷计算器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            color: #333;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center; /* 水平居中 */
            align-items: center; /* 垂直居中 */
            min-height: 100vh; /* 屏幕最小高度 */
            background-image: url("img/7.webp");
            background-size:550; /* 背景图片大小 */
            background-position: center; /* 背景图片位置 */
        }

        h1 {
            text-align: center;
            color: #5a5a5a;
        }

        h2 {
            color: #4a4a4a;
        }

        .container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            max-width: 600px;
            width: 100%; /* 确保容器在小屏幕上也能适应 */
        }

        p {
            margin: 10px 0;
        }

        input[type="text"], select {
            width: calc(100% - 20px);
            padding: 10px;
            margin: 5px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        button {
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            padding: 10px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
        }

        button:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #e9ecef;
            border-radius: 5px;
        }

        a {
            display: block; /* 使链接为块级，以便居中 */
            text-align: center; /* 链接文本居中 */
            margin-top: 10px; /* 给链接留一点间距 */
            color: #007bff; /* 链接颜色 */
            text-decoration: none; /* 去掉下划线 */
        }

        a:hover {
            text-decoration: underline; /* 悬停时显示下划线 */
        }

        footer {
            text-align: center; /* 页脚居中 */
            margin-top: 20px; /* 给页脚留一点间距 */
        }
    </style>
    <script>
        function jisuan() {
            let jisuanfangshi = document.querySelector("#jisuanfangshi").value;
            let fangwuzongjia, anjiechengshu, daikuanzonge, shoufukuan, yuelilv, daikuanyears, huankuanyueshu, shenmi, meiyuehuankuan;

            if (jisuanfangshi == 1) {
                fangwuzongjia = document.querySelector("#fangwuzongjia").value;
                console.log("获取房屋总价：", fangwuzongjia);
                
                anjiechengshu = document.querySelector("#anjiechengshu").value;
                shoufukuan = fangwuzongjia * (1 - parseFloat(anjiechengshu)) * 10000;
                console.log("首付款：", shoufukuan);
                daikuanzonge = fangwuzongjia * parseFloat(anjiechengshu) * 10000;
                console.log("贷款总额：", daikuanzonge);
                
                yuelilv = parseFloat(document.querySelector("#shangdaililv").value) / 100 / 12;
                yuelilv = parseFloat(yuelilv.toFixed(4));
                console.log("月利率:", yuelilv);
                
                daikuanyears = parseInt(document.querySelector("#daikuanyears").value);
                console.log("按揭年数:", daikuanyears);
                huankuanyueshu = daikuanyears * 12;
                console.log("还款月数:", huankuanyueshu);
                
                shenmi = Math.pow(1 + yuelilv, huankuanyueshu);
                meiyuehuankuan = daikuanzonge * yuelilv * shenmi / (shenmi - 1);
                console.log("每月还款额:", meiyuehuankuan);
                
                let totalRepayment = meiyuehuankuan * huankuanyueshu;
                console.log("还款总额:", totalRepayment);
                
                document.getElementById("yuehuankuan").value = meiyuehuankuan.toFixed(2);
                document.getElementById("hkze").value = totalRepayment.toFixed(2);
                document.getElementById("sfk").value = shoufukuan.toFixed(2);
                document.getElementById("dkze").value = daikuanzonge.toFixed(2);
            }
        }

        function initDaikuanyears() {
            let daikuanyearsSelect = document.querySelector("#daikuanyears");
            for(let i=1; i<=30; i++){
                let option = document.createElement("option");
                option.value = i;
                option.text = i + "年(" + (i * 12) + "期)";
                daikuanyearsSelect.appendChild(option);
            }
        }

        window.onload = initDaikuanyears;
    </script>
</head>
<body>
    
    <div class="container">
        <h1>房贷公积金贷款商贷组合贷款计算器</h1>
        <h2>房贷信息</h2>
        <p>贷款类别：
            <select id="daikuanleixing">
                <option value="1">商贷</option>
                <option value="2">公积金贷款</option>
                <option value="3">组合贷款</option>
            </select>
        </p>
        <p>
            还款方式：
            <select id="huankuanfangshi">
                <option value="1">等额本息</option>
                <option value="2">等额本金</option>
            </select>
        </p>
        <p>
            计算方式：<select id="jisuanfangshi">
                <option value="1">根据按揭成数计算</option>
                <option value="2">根据贷款总额计算</option>
            </select>
        </p>
        <p>
            房屋总价：<input type="text" id="fangwuzongjia" placeholder="请输入房屋总价"> 万元
        </p>
        <p>
            按揭成数：<select id="anjiechengshu">
                <option value="0.9">9成</option>
                <option value="0.8">8成</option>
                <option value="0.7" selected>7成</option>
                <option value="0.6">6成</option>
                <option value="0.5">5成</option>
                <option value="0.4">4成</option>
                <option value="0.3">3成</option>
                <option value="0.2">2成</option>              
            </select>  
        </p>
        <p>
            按揭年数：<select id="daikuanyears"></select>
        </p>
        <p>
            商贷利率：<input type="text" id="shangdaililv" placeholder="请输入商贷利率"> %
        </p>
        <P><button onclick="jisuan()">房贷计算</button></P>
        <div class="result">
            <p>首付款：<input type="text" id="sfk" readonly> 万元</p>
            <p>贷款总额：<input type="text" id="dkze" readonly> 万元</p>
            <p>还款总额：<input type="text" id="hkze" readonly> 元</p>
            <p>月均还款：<input type="text" id="yuehuankuan" readonly> 元</p>
            <p><a href="ceshihe.html">点击返回首页</a></p>
            <footer>© 2024 周露航</footer>
        </div>
        
    </div>
    
</body>
</html>
